/* ************************************************************************

   Copyright:

   License:

   Authors: SALMON Charles

************************************************************************ */

/**
 * Extend the qooxdoo Json store to adapt it to our JSON-RPC layer
 */
qx.Class.define("bip.rpc.Json", 
{
	extend : qx.data.store.Json,
	
	members :
	  {
		/**
	     * Creates and sends a JSON-RPC request. Additionally two
	     * listeners will be added for the state and the completed event of the
	     * request.
	     *
	     * @param url {String} The url for the request.
	     */
	    _createRequest: function(url) {
	    	var self = this;
	      // check for the request configuration hook
	      var del = this._delegate;
	      if (del && qx.lang.Type.isFunction(del.configureRequest)) {
	        this._delegate.configureRequest(this);
	      }
	      if (url == null) {return;}
	      // Create topic
	      if (url.match("topics/createTopic")) {
	      	try {
	      		bip.rpc.RpcManager.getInstance().getJsonRpc().TopicManager.createTopic(
			    			function(result,error) {self.__jsonCompleteHandler.apply(self,arguments);},
			    			this.getModel().getName(), this.getModel().getDescription(), this.getModel().getIdlocal()
			    	);
	      	} catch(e) {
	      		alert("JSON-RPC problem:"+e);
	      	}
	      } 
	      else if (url.match("questions/createQuestion")) {
	      	try {
	      		bip.rpc.RpcManager.getInstance().getJsonRpc().QuestionManager.createQuestion(
			    			function(result,error) {self.__jsonCompleteHandler.apply(self,arguments);},
			    			this.getModel().getTopicid(),
			    			this.getModel().getName(), this.getModel().getDescription(), this.getModel().getIdlocal(),
			    			this.getModel().getMinvalue(),this.getModel().getMaxvalue(),this.getModel().getDefaultvalue()
			    	);
	      	} catch(e) {
	      		alert("JSON-RPC problem:"+e);
	      	}
	      } 
	      else {
	      	alert("ERROR: Unknown json-rpc request:"+url);
	      }
	    },
	    
	    /**
	     * Handler for the completion of the requests. It invokes the creation of
	     * the needed classes and instances for the fetched data using
	     * {@link qx.data.marshal.Json}.
	     *
	     * @param res {Map} The result 
	     */
	    __jsonCompleteHandler : function(res, e)
	    {
	    	if (res == null) {
	    		this.setState("failed");
		      var exception = new bip.rpc.RpcException();
		      exception.setName(e.name);
		      exception.setCode(e.code);
		      exception.setMessage(e.message);
		      //exception.setTrace(e.javaStack);
		      this.fireDataEvent("error",exception);
				  return;
	    	}
	    	this.setState("completed");
	    	
	    	var data = res;
	    	// check for the data manipulation hook
	    	var del = this._delegate;
	    	if (del && qx.lang.Type.isFunction(del.manipulateData)) {
	    		data = this._delegate.manipulateData(data);
	    	}

	    	// create the class
	    	this._marshaler.toClass(data, true);

	    	var oldModel = this.getModel();

	    	// set the initial data
	    	this.setModel(this._marshaler.toModel(data));

	    	// get rid of the old model
	    	if (oldModel && oldModel.dispose) {
	    		oldModel.dispose();
	    	}

	    	// fire complete event
	    	this.fireDataEvent("loaded", this.getModel());
	    }
	  }
});